<?php

class Client_Table_Gallery extends Apk_Db_Table
{
	protected $_name      = 'gallery';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }
    
    public function GetDataList($page=1, $sort=null, $page_count=null, $params=null)
    {    	
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name)   
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');    	

		if(count($params) > 0)
		foreach($params as $k => $v)
		{
			switch ($k)		
			{					
				case 'cats':
					if(count($v) > 0)
					$sel->where('id_cat in (?)', $v);
					break;				
				case 'is_active':
					if(intval($v) >= 0 && trim($v) != '')
					$sel->where($this->_name.'.'.$k.' = ?', intval($v));							
					break;	
				default:
					if(intval($v) > 0)
					$sel->where($this->_name.'.'.$k.' = ?', $v);		
			}				
		}		 
			
    		  
		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.add_date desc');	
				
    		  
    	return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);    	

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    } 
    
    
    public function GetAccessList($a = null, $lang = 'ru')
    {
    	$ret = array();

		$sel = $this->getAdapter()->select()
					->from('access_level', array('id', 'title' => 'title_'.$lang))
					->order('title_ru');		
					
		$data = $this->getList($sel)->toArray();

    	if(count($data) > 0 && !is_null($a))
    	foreach($data as $k => $v)
    	  	if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    } 
  
    
    //*******************
    // insert/update/delete

	public function insert(array $data, $user_id)
    {    
    	$data['modify_date'] = time();
    	$data['add_date'] = time();
    	$data['add_user_id'] = $user_id;
    	$data['modify_user_id'] = $user_id;
    	
        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id)
    {    	
    	$data['modify_date'] = time();    	
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	if(!isset($data['is_active'])) $data['is_active'] = 0;  	    
    	
		return parent::update($data, 'id = '.$id);
    }

    public function remove($id, $user_id)
    {    	
    	
    	$data['modify_user_id'] = $user_id; 
    	$data['modify_date'] = time();
    	$data['is_removed'] = 1;
    	
    	parent::update($data, 'id = '.$id);    		    	
    	
    	return true;    	
	}
	
    public function remove_commetns($items, $aid)
    {    	
    	foreach($items as $k => $id)
    	{	    	
	    	$this->getAdapter()->delete('articles_comments', 'id = '.$id);    		
    	}
    	
    	$this->commetns_count($aid);
    	
    	return true;    	
	}
	
	
    public function commetns_count($id)   
    {
    	$sel = $this->getAdapter()->select()
    					->from('articles_comments', array('count' => 'count(id)'))
    					->where('id_article = ?', $id);
    	$count = $this->getList($sel)->getRow(0)->toArray();  
    	
    	$d['comments_count'] = $count['count'];
    	parent::update($d, 'id = '.$id);  	
    }
    
    public function photo_count($id)   
    {
    	$sel = $this->getAdapter()->select()
    					->from('objects', array('count' => 'count(id)'))
    					->where('id_gallery = ?', $id)
    					->where('is_active')
    					->where('is_ghost = 0')
    					->where('is_removed = 0');
    					
    	$count = $this->getList($sel)->getRow(0)->toArray();  
    	
    	$d['count_photo'] = $count['count'];
    	parent::update($d, 'id = '.$id);  	
    }    	

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}
?>